from fastapi import APIRouter, Depends
from schema.resp import RestfulModel
from dependencies import get_mongo_db_of_condition_safety
from typing import List
from pymongo.database import Database
import utils
from random import randint
import hashlib
from datetime import datetime, timedelta, date


vehicle_condition_safety_router = APIRouter(prefix='/condition_safety/screen', tags=['车辆状态安全'])


class MongoCollectionName:
    """
    mongo 中 collection 的 name 常量
    """
    ACCELERATOR_PEDAL='Abnormal_accelerator_pedal'
    VEHICLE_CURRENT='Abnormal_vehicle_current'
    BRAKE_PEDAL='Abnormal_brake_pedal'
    SOC_ABNORMALITY='SOC_abnormality'
    VEHICLE_POWER_CONSUMPTION='Abnormal_vehicle_power_consumption'
    CAR_MAP='Car_map'
    VEHICLE_TYPE='Vehicle_type_statistics'
    STATUS_STSTISTICS='Abnormal_state_count'
    ATTACK_DISPLAY='Attack_situation_display'
    ATTACK_STATISTICS='Attack_Statistics'


#地图
@vehicle_condition_safety_router.get('/map_info',summary='地图')
async def get_map_info(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.CAR_MAP]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)


#车型接口
@vehicle_condition_safety_router.get('/car_type',summary='车型接口')
async def get_car_type(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.VEHICLE_TYPE]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)

#车辆耗电异常展示
@vehicle_condition_safety_router.get('/car_electron',summary='车辆耗电异常展示')
async def get_car_electron(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.VEHICLE_POWER_CONSUMPTION]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)

#车辆电流异常
@vehicle_condition_safety_router.get('/car_electron_stream',summary='车辆电流异常')
async def get_car_electron_stream(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.VEHICLE_CURRENT]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)

#加速踏板异常
@vehicle_condition_safety_router.get('/speed_exce',summary='加速踏板异常')
async def get_speed_exce(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.ACCELERATOR_PEDAL]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)


#制动踏板异常
@vehicle_condition_safety_router.get('/stop_exce',summary='制动踏板异常')
async def get_stop_exce(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.BRAKE_PEDAL]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)

#SOC异常
@vehicle_condition_safety_router.get('/soc_exce',summary='SOC异常')
async def get_soc_exce(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.SOC_ABNORMALITY]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)

#异常状态统计
@vehicle_condition_safety_router.get('/exce_status',summary='异常状态统计')
async def get_exce_status(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.STATUS_STSTISTICS]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)


#攻击态势展示
@vehicle_condition_safety_router.get('/attack_show',summary='攻击态势展示')
async def get_attack_show(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.ATTACK_DISPLAY]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)


#车辆攻击统计
@vehicle_condition_safety_router.get('/car_attack',summary='车辆攻击统计')
async def get_car_attack(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    map_info_col = mongo_db[MongoCollectionName.ATTACK_STATISTICS]
    cursor = map_info_col.find({},{ '_id':0})
    result=[]
    for u in cursor:
        result.append(u)
    return RestfulModel.success(result)



@vehicle_condition_safety_router.get('/car-num-tendency',summary='车辆数量趋势')
async def get_car_num_tendency():
    now = datetime.now()
    today = date(now.year, now.month, now.day)
    delta = timedelta(days=1)
    result = []
    num = 327690
    for i in range(7):
        t = today - i * delta
        user_num_tendency={
            'time':t,
            'value':num
        }
        result.append(user_num_tendency)
        rand_val = int.from_bytes(hashlib.md5(str(t).encode()).digest(), byteorder='big') % 200 + 100
        num -= rand_val
    result.reverse()
    return RestfulModel.success(result)